package com.oujia.cloud.cloudcommon.bean.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.oujia.cloud.cloudcommon.api.APIResultEnum;
import com.oujia.cloud.cloudcommon.bean.exception.ServiceException;
import com.oujia.cloud.cloudcommon.util.JSONUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 短信发送工具类
 */
@Component
@Log4j
public class MsgSendUtils {
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    private static String accessKeyId;

    private static String accessKeySecret;

    public static String getAccessKeyId() {
        return accessKeyId;
    }

    @Value("${aliyun.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public static String getAccessKeySecret() {
        return accessKeySecret;
    }

    @Value("${aliyun.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public static SendSmsResponse sendSms(String phone, Map map, String templateCode) {
        SendSmsResponse sendSmsResponse = null;

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
        System.setProperty("sun.net.client.defaultReadTimeout", "5000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            throw new ServiceException(APIResultEnum.PHONE_ALI_SEND);
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName("偶家科技");
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);

        //可选:亲，您订购的宝贝已乘坐${name}快递，${order}号火箭迫不及待奔向您了，请留意签收。签收宝贝时请开箱验货哦，如有问题请第一时间拍照并联系客服电话0755-83732284-8008，感谢您的支持。
        request.setTemplateParam(JSONUtil.toJson(map));
        //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        //hint 此处可能会抛出异常，注意catch
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            throw new ServiceException(APIResultEnum.PHONE_ALI_SEND);
        }
        log.info("阿里短信发送返回" + JSONUtil.toJson(sendSmsResponse));
        //短信限流异常处理
        if (!sendSmsResponse.getCode().equalsIgnoreCase("OK")) {
            if ("isv.BUSINESS_LIMIT_CONTROL".equals(sendSmsResponse.getCode())) {
                throw new ServiceException(APIResultEnum.USER_MESSAGE_LIMIT);
            }
            if ("isv.MOBILE_NUMBER_ILLEGAL".equals(sendSmsResponse.getCode())) {
                throw new ServiceException(APIResultEnum.PHONE_NUMBER_ILLEGAL);
            }
            throw new ServiceException(APIResultEnum.PHONE_NUMBER_ILLEGAL.getCode(),sendSmsResponse.getMessage());
        }
        return sendSmsResponse;
    }


}
